package com.hzy.alibaba.controller;

import com.google.common.collect.Lists;
import com.hzy.alibaba.entity.primary.User;
import com.hzy.alibaba.entity.salve.SalveUser;
import com.hzy.alibaba.exception.OperationException;
import com.hzy.alibaba.service.SalveUserService;
import com.hzy.alibaba.service.UserService;
import com.hzy.alibaba.utils.RedisUtil;
import com.hzy.alibaba.config.redisson.RedissonLocker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SalveUserService salveUserService;
    @Autowired
    private ValueOperations valueOperations;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedissonLocker redissonLocker;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Page<User> page() {
        return userService.findAll();
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public User selectByKey(@PathVariable Long id) {
        return userService.selectByKey(id);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public void save(@RequestBody @Valid User user) throws Exception {
        SalveUser salveUser = new SalveUser();
        BeanUtils.copyProperties(user, salveUser);
        userService.save(user);
        salveUserService.save(salveUser);
        throw new OperationException("测试异常");
    }

    @RequestMapping(value = "/list/{userName}", method = RequestMethod.GET)
    public List<Object> getList(@PathVariable String userName) {
        return Lists.newArrayList(userService.findUserByUserName(userName),
                salveUserService.findUserByUserName(userName));
    }

    @RequestMapping(value = "/{userName}", method = RequestMethod.GET)
    public User getUserByName(@PathVariable String userName) {
        return userService.findUserByUserName(userName);
    }

    @RequestMapping(value = "set", method = RequestMethod.GET)
    public void set() {

        User userByUserName = userService.findUserByUserName("abe");

        valueOperations.set("user", userByUserName);
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public User get() {

        return (User) valueOperations.get("user");
    }

    @RequestMapping(value = "get2", method = RequestMethod.GET)
    public User get2() {

        return (User) redisUtil.get("user");
    }

    @RequestMapping(value = "redisson", method = RequestMethod.GET)
    public String redisson() throws Exception {
        Long start = System.currentTimeMillis();
        System.out.println("------------start--------------");
        String synchronize = redissonLocker.synchronize("123123", (t) -> {
            try {
                System.out.println(t);
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "200";
        });
        System.out.println("-------------end--------------" + (System.currentTimeMillis() - start));
        return synchronize;
    }
}
